package com.disktalk.api;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.csource.common.MyException;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;

public class FastDFS {
	
	private final static ExecutorService pool = Executors.newFixedThreadPool(10);

//	/**
//	 * 异步处理上传
//	 * @param fileName
//	 * @return
//	 */
//	public static Future<String[]> FutureUpload(String fileName) {
//
//		return pool.submit(new Callable<String[]>() {
//	        @Override
//	        public String[] call() throws Exception {        	
//	        		return Upload(fileName, null, null);
//	            
//	        }
//	    });
//	}
//	
//	/**
//	 * 异步处理上传
//	 * @param file_buff
//	 * @param nvp
//	 * @param extName
//	 * @return
//	 */
//	public static Future<String[]> FutureUpload(byte[] file_buff, NameValuePair nvp[], String extName) {
//
//		return pool.submit(new Callable<String[]>() {
//	        @Override
//	        public String[] call() throws Exception {
//	        	
//	        		return Upload(file_buff, nvp, extName);	            
//	        }
//	    });
//	}
//	
	/**
	 * 上传文件
	 * 
	 * @param fileName
	 *            文件路径， 例如 D:\\Linux\\nginx.txt
	 */
	public static String[] Upload(String fileName) {

		return Upload(fileName, null, null);
	}


	public static String[] Upload(NameValuePair nvp[], String fileName) {

		return Upload(fileName, nvp, null);
	}

	/**
	 * 上传文件
	 * 
	 * @param fileName
	 *            文件路径， 例如 D:\\Linux\\nginx.txt
	 * @param nvp附加参数，例如
	 *            NameValuePair nvp [] = new NameValuePair[]{ new
	 *            NameValuePair("age", "18"), new NameValuePair("sex", "male")
	 *            };
	 * @param extName
	 *            文件扩展名
	 * @return
	 */
	public static String[] Upload(String fileName, NameValuePair nvp[], String extName) {

		try {
			ClientGlobal.init(Config.CFGFILE);

			TrackerClient tracker = new TrackerClient();
			TrackerServer trackerServer = tracker.getConnection();
			StorageServer storageServer = null;

			StorageClient storageClient = new StorageClient(trackerServer, storageServer);

			return storageClient.upload_file(fileName, extName, nvp);

		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		} catch (MyException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 
	 * @param file_buff 文件缓冲
	 * @param nvp附加参数，例如
	 *            NameValuePair nvp [] = new NameValuePair[]{ new
	 *            NameValuePair("age", "18"), new NameValuePair("sex", "male")
	 *            };
	 * @param extName
	 *            文件扩展名
	 * @return
	 */
	public static String[] Upload(byte[] file_buff, NameValuePair nvp[], String extName) {

		try {
			ClientGlobal.init(Config.CFGFILE);

			TrackerClient tracker = new TrackerClient();
			TrackerServer trackerServer = tracker.getConnection();
			StorageServer storageServer = null;

			StorageClient storageClient = new StorageClient(trackerServer, storageServer);

			return storageClient.upload_file(file_buff, extName, nvp);

		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		} catch (MyException e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 下载获得byte数组
	 * 
	 * @param groupName
	 *            组名，比如"group1"
	 * @param remoteFileName
	 *            文件名，比如"M00/00/3F/wKgrMlkdx7-AJIZgAAA-5vvGciE98902_big.c"
	 * @return
	 */
	public static byte[] Download(String groupName, String remoteFileName) {
		try {

			ClientGlobal.init(Config.CFGFILE);

			TrackerClient tracker = new TrackerClient();
			TrackerServer trackerServer = tracker.getConnection();
			StorageServer storageServer = null;

			StorageClient storageClient = new StorageClient(trackerServer, storageServer);

			return storageClient.download_file(groupName, remoteFileName);

		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

	}

	/**
	 * 直接下载到硬盘
	 * 
	 * @param groupName
	 *            组名，比如"group1"
	 * @param remoteFileName
	 *            文件名，比如"M00/00/3F/wKgrMlkdx7-AJIZgAAA-5vvGciE98902_big.c"
	 * @param saveTo
	 *            下载到硬盘的文件名，比如"d:\\"+ UUID.randomUUID().toString()+".c"
	 */
	public static void Download(String groupName, String remoteFileName, String saveTo) {

		try {
			FileOutputStream out = new FileOutputStream(saveTo);
			out.write(Download(groupName, remoteFileName));
			out.close();
		} catch (IOException e) {
			// do something
		}
	}

	/**
	 * 删除文件
	 * 
	 * @param groupName
	 *            组名，比如"group1"
	 * @param remoteFileName
	 *            文件名，比如"M00/00/3F/wKgrMlkdx7-AJIZgAAA-5vvGciE98902_big.c"
	 * @return 0 则删除成功
	 */
	public static int Delete(String groupName, String remoteFileName) {
		try {
			ClientGlobal.init(Config.CFGFILE);

			TrackerClient tracker = new TrackerClient();
			TrackerServer trackerServer = tracker.getConnection();
			StorageServer storageServer = null;

			StorageClient storageClient = new StorageClient(trackerServer, storageServer);
			int i = storageClient.delete_file(groupName, remoteFileName);
			return i;

		} catch (Exception e) {
			e.printStackTrace();
			return 1; // 删除失败
		}
	}

}
